{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 加载必要的包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import netCDF4 as nc"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 读取nc文件\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "nc_data = nc.Dataset(\"lftx.sfc.2021.nc\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 查看nc文件的维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dimension name: lat, dimension size: 73\n",
      "dimension name: lon, dimension size: 144\n",
      "dimension name: time, dimension size: 306\n",
      "dimension name: nbnds, dimension size: 2\n"
     ]
    }
   ],
   "source": [
    "for temp_dim in nc_data.dimensions.values():\n",
    "    print(f\"dimension name: {temp_dim.name}, dimension size: {temp_dim.size}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 查看nc文件的变量\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "********************************************************************** \n",
      "\n",
      "<class 'netCDF4._netCDF4.Variable'>\n",
      "float32 lat(lat)\n",
      "    units: degrees_north\n",
      "    actual_range: [ 90. -90.]\n",
      "    long_name: Latitude\n",
      "    standard_name: latitude\n",
      "    axis: Y\n",
      "unlimited dimensions: \n",
      "current shape = (73,)\n",
      "filling on, default _FillValue of 9.969209968386869e+36 used\n",
      "********************************************************************** \n",
      "\n",
      "<class 'netCDF4._netCDF4.Variable'>\n",
      "float32 lon(lon)\n",
      "    units: degrees_east\n",
      "    long_name: Longitude\n",
      "    actual_range: [  0.  357.5]\n",
      "    standard_name: longitude\n",
      "    axis: X\n",
      "unlimited dimensions: \n",
      "current shape = (144,)\n",
      "filling on, default _FillValue of 9.969209968386869e+36 used\n",
      "********************************************************************** \n",
      "\n",
      "<class 'netCDF4._netCDF4.Variable'>\n",
      "float64 time(time)\n",
      "    long_name: Time\n",
      "    delta_t: 0000-00-01 00:00:00\n",
      "    standard_name: time\n",
      "    axis: T\n",
      "    units: hours since 1800-01-01 00:00:0.0\n",
      "    avg_period: 0000-00-01 00:00:00\n",
      "    coordinate_defines: start\n",
      "    actual_range: [1937256. 1944576.]\n",
      "unlimited dimensions: time\n",
      "current shape = (306,)\n",
      "filling on, default _FillValue of 9.969209968386869e+36 used\n",
      "********************************************************************** \n",
      "\n",
      "<class 'netCDF4._netCDF4.Variable'>\n",
      "float32 lftx(time, lat, lon)\n",
      "    long_name: mean Daily Surface Lifted Index\n",
      "    units: degK\n",
      "    precision: 2\n",
      "    least_significant_digit: 1\n",
      "    GRIB_id: 131\n",
      "    GRIB_name: LFT_X\n",
      "    var_desc: Surface Lifted Index\n",
      "    dataset: NCEP Reanalysis Daily Averages\n",
      "    level_desc: Surface\n",
      "    statistic: Mean\n",
      "    parent_stat: Individual Obs\n",
      "    missing_value: -9.96921e+36\n",
      "    valid_range: [-18.  70.]\n",
      "    actual_range: [-11.85   52.475]\n",
      "unlimited dimensions: time\n",
      "current shape = (306, 73, 144)\n",
      "filling on, default _FillValue of 9.969209968386869e+36 used\n",
      "********************************************************************** \n",
      "\n",
      "<class 'netCDF4._netCDF4.Variable'>\n",
      "float64 time_bnds(time, nbnds)\n",
      "unlimited dimensions: time\n",
      "current shape = (306, 2)\n",
      "filling on, default _FillValue of 9.969209968386869e+36 used\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-4-0cd40c49039b>:4: DeprecationWarning: tostring() is deprecated. Use tobytes() instead.\n",
      "  print(temp_value)\n"
     ]
    }
   ],
   "source": [
    "for temp_value in nc_data.variables.values():\n",
    "    print(\"*\" * 70, '\\n')\n",
    "    # print(f\"variable name: {temp_value.name}, variable shape: {temp_value.shape}\")\n",
    "    print(temp_value)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 提取变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-5-e18014418500>:1: DeprecationWarning: `np.bool` is a deprecated alias for the builtin `bool`. To silence this warning, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.\n",
      "Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
      "  process_data = np.array(nc_data.variables['lftx'])  # (time, lat, lon)\n"
     ]
    },
    {
     "data": {
      "text/plain": "(306, 73, 144)"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "process_data = np.array(nc_data.variables['lftx'])  # (time, lat, lon)\n",
    "process_data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "(time, lat, lon) = process_data.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 数据处理\n",
    "我们求各个位置的平均数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 73/73 [00:00<00:00, 1378.95it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "result data shape: (73, 144)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "result_data = np.zeros(shape=(lat, lon))\n",
    "\n",
    "from tqdm import tqdm\n",
    "\n",
    "for temp_lat in tqdm(range(lat)):\n",
    "    for temp_lon in range(lon):\n",
    "        result_data[temp_lat, temp_lon] = process_data[:, temp_lat, temp_lon].sum()\n",
    "\n",
    "print(f\"result data shape: {result_data.shape}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-8-63bd4a3b8296>:1: DeprecationWarning: `np.bool` is a deprecated alias for the builtin `bool`. To silence this warning, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.\n",
      "Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
      "  lat_data = np.array(nc_data.variables['lat'])  # degrees_north\n",
      "<ipython-input-8-63bd4a3b8296>:3: DeprecationWarning: `np.bool` is a deprecated alias for the builtin `bool`. To silence this warning, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.\n",
      "Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
      "  lon_data = np.array(nc_data.variables['lon'])  # degrees_east\n"
     ]
    }
   ],
   "source": [
    "lat_data = np.array(nc_data.variables['lat'])  # degrees_north\n",
    "\n",
    "lon_data = np.array(nc_data.variables['lon'])  # degrees_east"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "(73, 144)"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Lon_data, Lat_data = np.meshgrid(lon_data, lat_data)\n",
    "\n",
    "Lon_data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using matplotlib backend: MacOSX\n"
     ]
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "%matplotlib\n",
    "fig = plt.figure()\n",
    "ax = fig.add_subplot(projection='3d')\n",
    "\n",
    "ax.plot_surface(Lon_data, Lat_data, result_data, cmap=plt.cm.YlGnBu_r)\n",
    "\n",
    "ax.set_xlabel(r'Longitude (degrees_east)')\n",
    "ax.set_ylabel(r'Latitude (degrees_north)')\n",
    "ax.set_zlabel(r'sum (degK)')\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%"
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "language": "python",
   "display_name": "Python 3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}